var id = 0;
var id1 = 0;
var t = 0; /*成功的次数*/
var y = 0; /*失败的次数*/
var z = 0; /*总个数*/
var winnum = 20;
var losenum = 20;
var addtime = 2000;
var movetime = 100;
var gameLV=1;

function kaishi() {
	id = setInterval("creatPG()", addtime)
	id1 = setInterval("movePG()", movetime)
}

function zanting() {
	clearInterval(id);
	clearInterval(id1);
	id = 0;
	id1 = 0;
}

function jieshu() {
	document.getElementById("top1").innerHTML = ""
	document.getElementById("p1").innerHTML = "0"
	document.getElementById("p2").innerHTML = "0"
	document.getElementById("p3").innerHTML = "0"
	document.getElementById("p4").innerHTML = "1"
	clearInterval(id);
	clearInterval(id1);
	id = 0;
	id1 = 0;
}

function shezhi() {
	document.getElementById("shezhi_jiemian").style.display = "block";
	document.getElementById("top1").innerHTML = ""
	document.getElementById("p1").innerHTML = "0"
	document.getElementById("p2").innerHTML = "0"
	document.getElementById("p3").innerHTML = "0"
	document.getElementById("p4").innerHTML = "1"
	clearInterval(id);
	clearInterval(id1);
	id = 0;
	id1 = 0;
}

function queding() {
	gameLV = (document.getElementById("LVnum").value) * 1;
	winNum = document.getElementById("passNum").value;
	loseNum = document.getElementById("nopassNum").value;
	var nandu = document.getElementById("p4");
	nandu.innerHTML = gameLV;
	pd();

	document.getElementById("shezhi_jiemian").style.display = "none"

}
function pd(){
	switch(gameLV) {
		case 1:
			addtime = 2000;
			movetime = 100;
			break;
		case 2:
			addtime = 1400;
			movetime = 60;
			break;
		case 3:
			addtime = 1000;
			movetime = 30;
			break;
		case 4:
			addtime = 600;
			movetime = 10;
			break;
		default:
			alert("目前难度只有1到4");
			moren();
			nandu.innerHTML = 1;
			break;
	}
}
function quxiao() {
	document.getElementById("shezhi_jiemian").style.display = "none";
}

function moren() {
	document.getElementById("LVnum").value = "1";
	document.getElementById("passNum").value = "50";
	document.getElementById("nopassNum").value = "50";
}

function creatPG() {
	var div = document.createElement("div")
	var num = parseInt(Math.random() * 26) + 65;
	div.innerHTML = "&#" + num + ";"
	var x = div.getAttribute("x")
	var left1 = parseInt(Math.random() * 700);
	div.style.left = left1 + "px"
	div.style.top = "0px"
	div.setAttribute("x", num)
		//				console.log(div.getAttribute("x"))
	document.getElementById("top1").appendChild(div);
	z++;
	var zql = document.getElementById("zhengquelv");
	p3.innerHTML = parseInt(t / z * 100)
}

function movePG() {

	var p2 = document.getElementById("p2")
	var top1 = document.getElementById("top1")
	for(i = 0; i < top1.children.length; i++) {
		var x = parseInt(top1.children[i].style.top);
		x += 10;
		top1.children[i].style.top = x + "px";
		if(x > 380) {
			top1.children[i].className = "psPG"
		}
		if(x > 400) {
			top1.removeChild(top1.children[i])
			y++;
		}
		//失败次数
		p2.innerHTML = y;
		if(y == 20) {
			alert("游戏结束")
			y=0;
			document.getElementById("top1").innerHTML = ""
			document.getElementById("p1").innerHTML = "0"
			document.getElementById("p2").innerHTML = "0"
			document.getElementById("p3").innerHTML = "0"
			document.getElementById("p4").innerHTML = "1"
			clearInterval(id);
			clearInterval(id1);
			id = 0;
			id1 = 0;
		}
	}

}

function clickPG(T) {
	var top1 = document.getElementById("top1")
	var p1 = document.getElementById("p1")

	for(i = 0; i < top1.children.length; i++) {
		if(top1.children[i].getAttribute("x") == T.keyCode) {
			top1.removeChild(top1.children[i])
			t++;

			break;
		}
	}
	//成功次数
	p1.innerHTML = t;
	if(t==20){
		alert("恭喜过关")
		t=0;
			document.getElementById("top1").innerHTML = ""
			document.getElementById("p1").innerHTML = "0"
			document.getElementById("p2").innerHTML = "0"
			document.getElementById("p3").innerHTML = "0"
			document.getElementById("p4").innerHTML = gameLV+=1;
			clearInterval(id);
			clearInterval(id1);
			id = 0;
			id1 = 0;
			gameLV+=1;
			
			pd();
	}
	switch(t) {
		case 5:
			document.getElementById("a1").style.display = "block";
			break;
		case 10:
			document.getElementById("a2").style.display = "block";
			break;
		case 15:
			document.getElementById("a3").style.display = "block";
			break;
		case 20:
			document.getElementById("a4").style.display = "block";
			break;
		case 25:
			document.getElementById("a5").style.display = "block";
			break;
		case 30:
			document.getElementById("a6").style.display = "block";
			break;
		case 35:
			document.getElementById("a7").style.display = "block";
			break;
		case 40:
			document.getElementById("a8").style.display = "block";
			break;
		case 45:
			document.getElementById("a9").style.display = "block";
			break;
		case 50:
			document.getElementById("a10").style.display = "block";
			break;
		default:
			break;
	}
}